package com.common.core.config;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;


@Configuration
public class EventBusConfig {

    @Bean(name = "eventBusExecutor")
    public Executor eventBusException() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setQueueCapacity(100);
        taskExecutor.setThreadNamePrefix("EventBus-AsyncEventBus");
        taskExecutor.initialize();
        return taskExecutor;
    }

    // 配置 AsyncEventBus，并使用 Spring 管理的线程池
    @Bean
    public AsyncEventBus asyncEventBus(@Qualifier("eventBusExecutor")Executor eventBusExecutor) {
        return new AsyncEventBus(eventBusExecutor);
    }

    // 可选配置同步 EventBus，如果需要同步事件支持
    @Bean
    public EventBus eventBus() {
        return new EventBus();
    }
}
